<?php
// 应用公共文件
use Firebase\JWT\JWT;
use \app\api\model\Users;
//校验jwt权限API
function verifyJwt($jwt = '')
{
    $key = "best_api";
    try {
        $jwtAuth = json_encode(JWT::decode($jwt, $key, array('HS256')));
        $authInfo = json_decode($jwtAuth, true);
        $api_token = Users::where('api_token', $jwt)->find();
        if (!empty($authInfo['user_id']) && !empty($api_token)) {
            $data = [
                'code' => 1001,
                'data' => [
                    'uid' => $authInfo['user_id'],
                    'message' => 'Token验证通过'
                ]
            ];
        } else {
            $data = [
                'code' => 1002,
                'data' => [
                    'message' => 'Token验证不通过,用户不存在'
                ]
            ];
        }
        return json_encode($data);
    } catch (\Firebase\JWT\ExpiredException $e) {
        return json_encode([
            'code' => 1003,
            'data' => ['message' => 'Token过期']
        ]);
    } catch (\Exception $e) {
        return json_encode([
            'code' => 1004,
            'data' => ['message' => 'Token无效']
        ]);
    }
}
